/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actioncombination.logic.policy;

import actioncombination.common.value.Value;
import actioncombination.logic.agent.action.Action;
import actioncombination.problem.gridworld.AgentStateImpl;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;

/**
 *
 * @author Administrador
 */
public class eGreedy extends PolicyStrategy {

    private float epsilon;

    public eGreedy(float epsilon) {
        this.epsilon = epsilon;
    }

    private float getEpsilon() {
        return epsilon;
    }

    private void setEpsilon(float epsilon) {
        this.epsilon = epsilon;
    }

    @Override
    public Action selectAction(Map<Action, Value> actionValues) {
        Random rnd = new Random(java.util.UUID.randomUUID().hashCode());
        Action maxAction = null;
        Value maxVal = null;
        ArrayList<Action> actions = (ArrayList<Action>) actionValues.keySet();

        if (rnd.nextFloat() > this.getEpsilon()) {
            for (Action a : actionValues.keySet()) {
                if (actionValues.get(a).compareTo(maxVal) > 0) {
                    maxAction = a;
                    maxVal = actionValues.get(a);
                }
            }
        } else {
            maxAction = actions.get(rnd.nextInt(actions.size()));
        }
        return maxAction;
    }

    public AgentStateImpl selectAction(Map<AgentStateImpl, Value> valuesStates) {
        Random rnd = new Random(java.util.UUID.randomUUID().hashCode());
        AgentStateImpl maxState = null;
        Value maxVal = null;
        ArrayList<AgentStateImpl> states = (ArrayList<AgentStateImpl>) valuesStates.keySet();

        if (rnd.nextFloat() > this.getEpsilon()) {
            for (AgentStateImpl s : valuesStates.keySet()) {
                if (valuesStates.get(s).compareTo(maxVal) > 0) {
                    maxState = s;
                    maxVal = valuesStates.get(s);
                }
            }
        } else {
            maxState = states.get(rnd.nextInt(valuesStates.size()));
        }
        return maxState;
    }

    @Override
    public Action selectAction(Map<Action, Value> actionValues, float epsilon) {
       this.setEpsilon(epsilon);
       return this.selectAction(actionValues);
    }

    @Override
    public AgentStateImpl selectAction(Map<AgentStateImpl, Value> valuesStates, float epsilon) {
        this.setEpsilon(epsilon);
        return this.selectAction(valuesStates);
    }

    @Override
    public void setParameter(float epsilon) {
        this.setEpsilon(epsilon);
    }

    @Override
    public float getParameter() {
        return this.getEpsilon();
    }
}
